package com.mf.controller.basic;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.mf.aspect.debounces.Debounce;
import com.mf.aspect.logApi.LogApi;
import com.mf.exception.GraceException;
import com.mf.groups.GroupsAddInterface;
import com.mf.groups.GroupsUpdateInterface;
import com.mf.result.Result;
import com.mf.utils.UtilConverter;
import com.mf.entity.req.basic.reqPublic.ReqPublicIds;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import com.mf.entity.dto.basic.DtoDic;
import com.mf.entity.req.basic.ReqDic;
import com.mf.service.basic.ServiceDic;
import com.mf.entity.req.basic.search.SearchReqDic;

import java.util.List;
import java.util.Map;

/**
 * Base-字典数据内容
 */
@RestController
@RequestMapping("dic")
@SaCheckLogin
@Tag(name = "Base-字典数据内容")
@Debounce
public class ControllerDic {
    @Resource
    private ServiceDic servicedic;

    @Operation(summary = "列表查询树结构")
    @PostMapping("/listTree")
    @SaCheckPermission("dic:search")
    public Result<List<DtoDic>> listTree(@RequestBody SearchReqDic searchReqDic) {
        return Result.success(servicedic.listTree(searchReqDic));
    }

    @Operation(summary = "根据pId查询Map集合数据字典")
    @PostMapping("/listByPid")
    public Result<Map<Integer, List<DtoDic>>> listByPid(@RequestBody ReqPublicIds<Integer> pIds) {
        return Result.success(servicedic.listByPid(pIds));
    }

    @Operation(summary = "保存")
    @PostMapping("/save")
    @LogApi("保存字典数据内容")
    @SaCheckPermission("dic:edit")
    public Result<Void> save(@Validated(GroupsAddInterface.class) @RequestBody ReqDic reqDic) {
        servicedic.save(UtilConverter.converter(reqDic, DtoDic.class));
        return Result.success();
    }

    @Operation(summary = "修改")
    @PostMapping("/update")
    @LogApi("修改字典数据内容")
    @SaCheckPermission("dic:edit")
    public Result<Void> update(@Validated(GroupsUpdateInterface.class) @RequestBody ReqDic reqDic) {
        servicedic.updateById(UtilConverter.converter(reqDic, DtoDic.class));
        return Result.success();
    }


    @Operation(summary = "删除")
    @GetMapping("/delete/{id}")
    @LogApi("删除字典数据内容")
    @SaCheckPermission("dic:delete")
    public Result<Void> delete(@PathVariable("id") Integer id) {
        GraceException.display(servicedic.countByPid(id) > 0, "有子节点不可直接删除");
        servicedic.removeById(id);
        return Result.success();
    }
}
